package cz.muni.fi.pa165.dao.impl;

import cz.muni.fi.pa165.dao.PackageDAO;
import cz.muni.fi.pa165.entity.Package;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
 * PackageDAO implemented class
 * @author Jakub Janco
 */
@Repository
public class PackageDAOImpl implements PackageDAO{

    @PersistenceContext
    private EntityManager em;

    @Override
    @Transactional
    public void removePackage(Package pkg) {
        checkPackage(pkg);
        em.remove(em.merge(pkg));
    }

    @Override
    @Transactional
    public Package getPackageById(Long id) {
        if(id == null) {
            throw new IllegalArgumentException("Id cannot be null");
        }
        return em.find(Package.class, id);
    }
    
    private void checkPackage(Package pkg) {
        if(pkg == null) {
            throw new IllegalArgumentException("Package cannot be null");
        }
        if(pkg.getId() == null) {
            throw new IllegalArgumentException("Package ID cannot be null");
        }
    }
}
